import {
	singleSaveRequest,
	getListRequest,
	updateRequest,
	deleteRequest,
	getfinishedListRequest
} from '../../api/driver/publish.js'

const state = {
	publishOrderList: [
		[],
		[],
		[]
	], // 运力发布list
	showPublishWindow: false, // 是否显示运力发布弹出窗
};

const mutations = {
	getPublishOrderList: (state, {
		data,
		page,
		orderState
	}) => {
		let index = 0;
		if (orderState === 1) {
			index = 2;
		}
		if (page === 1) {
			state.publishOrderList[index].splice(0)
		}
		state.publishOrderList[index].push(...data);
	},
	getPublishOrderFinishedList: (state, {
		data,
		page
	}) => {
		if (page === 1) {
			state.publishOrderList[1].splice(0)
		}
		state.publishOrderList[1].push(...data);
	},
	savePublishOrder: (state, data) => {
		// state.publishOrderList[0].unshift(data);
	},
	updatePublishOrder: (state, data) => {
		state.publishOrderList.splice(state.publishOrderList.findIndex(item => item.id === data.id), 1,
			data);
	},
	finishPublishOrder: (state,data) => {
		state.publishOrderList[1].splice(state.publishOrderList[1].findIndex(item => item.id === data.id), 1);
	},
	clearOrderList: state => {
		state.publishOrderList = [
			[],
			[],
			[]
		];
	},
	changeWindow: (state, show) => {
		state.showPublishWindow = show;
	},

};

const actions = {
	GETPUBLISHORDERLIST({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			getListRequest(data)
				.then(res => {
					if (res.header.code === 200) {
						commit('getPublishOrderList', {
							data: res.body.list,
							page: data.page,
							orderState: data.state
						});
					}
					if (res.body === '没有数据') {
						commit('clearOrderList');
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	GETPUBLISHORDERFINISHEDLIST({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			getfinishedListRequest(data)
				.then(res => {
					if (res.header.code === 200) {
						commit('getPublishOrderFinishedList', {
							data: res.body.list,
							page: data.page,
						});
					}
					if (res.body === '没有数据') {
						commit('clearOrderList');
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	SAVEPUBLISHORDER({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			singleSaveRequest({
					order: JSON.stringify(data)
				})
				.then(res => {
					if (res.header.code === 200) {
						// commit('savePublishOrder', data);
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	UPDATEPUBLISHORDER({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			updateRequest({
					order: JSON.stringify(data)
				})
				.then(res => {
					if (res.header.code === 200) {
						commit('updatePublishOrder', data);
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	FINISHPUBLISHORDER({commit},data) {
		return new Promise((resolve, reject) => {
			updateRequest({
					order: JSON.stringify(data)
				})
				.then(res => {
					if (res.header.code === 200) {
						commit('finishPublishOrder', data);
					}
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	DELETEORDER({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			deleteRequest(data)
				.then(res => {
					resolve(res.header);
				})
				.catch(err => {
					reject(err);
				})
		});
	},
	CLEARORDERLSIT({
		commit
	}) {
		commit('clearOrderList');
	},
	CHANGEWINDOW({
		commit
	}, data) {
		commit('changeWindow', data);
	}
};

export default {
	namespaced: true,
	mutations,
	actions,
	state
}
